package com.qingfeng.testandroid12sdkdemo.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.qingfeng.basic_lib.basic.BasicFragment
import com.qingfeng.testandroid12sdkdemo.R
import com.qingfeng.testandroid12sdkdemo.activity.TestActivity
import com.qingfeng.testandroid12sdkdemo.adapter.HomeListAdapter
import com.qingfeng.testandroid12sdkdemo.data.entity.ArticlesBean
import com.qingfeng.testandroid12sdkdemo.data.entity.BannerBean
import com.qingfeng.testandroid12sdkdemo.databinding.FragmentHomeBinding
import com.qingfeng.testandroid12sdkdemo.utils.GlideImageLoader
import com.youth.banner.Banner

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
/**
 * A simple [Fragment] subclass.
 * Use the [HomeFragment.newInstance] factory method to
 * create an instance of this fragment.
 */


//@AndroidEntryPoint
class HomeFragment : BasicFragment<HomeViewModel,FragmentHomeBinding>() {

    private val mHomeAdapter by lazy { HomeListAdapter() }
    private var page: Int = 0
    private lateinit var banner: Banner<BannerBean, GlideImageLoader>


    companion object {
        fun newInstance() = HomeFragment()
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding.homeSrl.setOnRefreshListener {
            dropDownRefresh()
        }



        with(mBinding.homeRv){
            layoutManager = LinearLayoutManager(context)
            adapter = mHomeAdapter
            banner = Banner(context)

            banner.minimumWidth = MATCH_PARENT
            banner.layoutParams =
                ViewGroup.LayoutParams(MATCH_PARENT, resources.getDimension(R.dimen.dp_120).toInt())
            banner.adapter = GlideImageLoader()
        }




        mHomeAdapter.apply {
            addHeaderView(banner)
            loadMoreModule.setOnLoadMoreListener(this@HomeFragment::loadMore)
            setOnItemClickListener { adapter,  _, position ->
                val item = adapter.data[position] as ArticlesBean
                val intent = Intent(context, TestActivity::class.java)
                intent.putExtra("url", item.link)
                startActivity(intent)
            }
        }
    }


    override fun lazyLoadData() {
        viewModel.run {
            getBanner().observe(this@HomeFragment, {
                banner.setDatas(it)
            })

            getHomeList(page).observe(this@HomeFragment, {
                if (mBinding.homeSrl.isRefreshing) {
                    mBinding.homeSrl.isRefreshing = false
                }
                it?.let {
                    if (it.curPage == 1) {
                        mHomeAdapter.setNewInstance(it.datas)
                    } else {
                        mHomeAdapter.addData(it.datas)
                    }
                    if (it.curPage == it.pageCount) {
                        mHomeAdapter.loadMoreModule.loadMoreEnd()
                    } else {
                        mHomeAdapter.loadMoreModule.loadMoreComplete()
                    }
                    page = it.curPage

                }
            })
        }
    }

    /**
     * 加载更多
     */
    private fun loadMore() {
        viewModel.getHomeList(page + 1)
    }

    /**
     * 下拉更多
     */
    private fun dropDownRefresh() {
        page = 0
        mBinding.homeSrl.isRefreshing = true
        viewModel.getHomeList(page, true)
        viewModel.getBanner(true)
    }

}